(function(){
	var $cat_dlg = $('#category_dlg');
	var elements = $('.etag');
	var $form = $(elements[0]);
	var $postId = $(elements[1]);
	var $title = $(elements[2]);
	var $tags = $(elements[3]);
	var $keyword = $(elements[4]);
	var $category = $(elements[5]);
	var $categorybtn = $(elements[6]);
	var $allowView = $(elements[7]);
	var $allowComment = $(elements[8]);
	var $abstract = $(elements[9]);
	var $postbtn = $(elements[10]);
	var $draftbtn = $(elements[11]);
	
	$(window).bind('beforeunload', function(event) {
		var hint = '您的日志还没有保存，确定要离开吗？';
		event.returnValue = hint;
		if($.browser.safari){
			return hint;
		}
	});
	
	$postbtn.click(function(){
		$.post('/post/edit/' + $postId.val() + '?p=true',getPostData(),function(result){
			document.location = '/post/prev/' + result.postId;
		})
	});
	$draftbtn.click(function(){
		$.post('/post/edit/' + $postId.val() + '?p=false',getPostData(),function(result){
			$postId.val(result.postId);
			alert('草稿保存成功');
		})
	});
	
	var cat_dlg_jst = TrimPath.parseTemplate(
			'<p><input type="text" class="dtag"><label><input class="dtag" type="checkbox">二级分类</label></p>\
			 <p class="hidden dtag">一级分类: <select class="dtag" style="width:140px;">{for x in cats}{if x.parentId==0}<option value="${x.id}">${x.name|escape}</option>{/if}{/for}</select></p>');
	
	var cat_jst = TrimPath.parseTemplate(
			'{for x in cats}<option value="${x.id}" {if x.id==classId} selected="selected"{/if}>{if x.parentId!=0}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{/if}${x.name|escape}</option>{/for}');
	
	fillCategory();
	
	function fillCategory(){
		$category.empty().append(cat_jst.process({cats:categorys,classId:postClassId}));
	}
	
	var $cat_dlg_itms = null;
	$cat_dlg.dialog({
		autoOpen:false,
		title:'新增分类',
		modal:true,
		open:function(){
			$cat_dlg.empty().append(cat_dlg_jst.process({cats:categorys}));
			$cat_dlg_itms = $('#category_dlg .dtag');
			$($cat_dlg_itms.get(1)).click(function(){
				$($cat_dlg_itms.get(2)).toggle();
			});
		},
		buttons:{
			'取消':function(){
				$cat_dlg.dialog('close');
			},
			'确认':function(){
				var className = $cat_dlg_itms.get(0).value;
				if(className.trim()==''){
					alert('请输入分类名称');
					return;
				}
				var isFirstClass = !$cat_dlg_itms.get(1).checked;
				var selectedClassId = $($cat_dlg_itms.get(3)).val();
				if(isFirstClass){
					selectedClassId = 0;
				}
				$.getJSON('/post/json/addClassName',{'className':className,'parentClassId':selectedClassId},function(data){
					if(!data){
						alert('添加的分类名已存在');
					}else{
						if(data.parentId==0){
							categorys.unshift(data);
						}else{
							for(var i=0;i<categorys.length;i++){
								if(data.parentId==categorys[i].id){
									categorys.splice(i+1,0,data);
									break;
								}
							}
							fillCategory();
						}
					}
					
				});
				$cat_dlg.dialog('close');
			}
		}});
	
	$categorybtn.click(function(){
		$cat_dlg.dialog('open');
	});
	
	function getPostData(){
		var data = {};
		data['postId'] = $postId.val();
		data['title'] = $title.val();
		data['content'] = editor.getContent();
		data['tags'] = $tags.val();
		data['keyword'] = $keyword.val();
		data['category'] = $category.val();
		data['publiced'] = !!$allowView.attr('checked');
		data['allowComment'] = !!$allowComment.attr('checked');
		data['digest'] = $abstract.val();
		return data;
	}
	var editor = new baidu.editor.ui.Editor();
	editor.render('editor');
	$('#editor').css({'display':''});
})();